package isp.apps.example.shoppingadmin.action;

import com.opensymphony.xwork.ActionSupport;

import isp.apps.example.shoppingcart.domain.CreditCard;
import isp.apps.example.shoppingcart.domain.Item;
import isp.apps.example.shoppingcart.domain.ShoppingCartValidator;
import isp.apps.example.shoppingcart.persistence.CreditCardDirectory;

import isp.apps.example.shoppingcart.persistence.CreditCardRepository;

import isp.apps.example.shoppingcart.persistence.factory.CreditCardPersistenceFactory;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class UpdateCreditCardsAction extends ActionSupport
{

  private String newCreditCardName, newSecurityLength;
  private Map creditCardNameMap, securityLengthMap, creditCardValidMap;
  private List allCreditCardsList;
  private CreditCardDirectory creditCardDir = CreditCardPersistenceFactory.getCreditCardDirectory();
  private CreditCardRepository ccRepository = CreditCardPersistenceFactory.getCreditCardRepository();
  
  
  public UpdateCreditCardsAction()
  {
  }
  
  public String execute()
  {
    allCreditCardsList = creditCardDir.getCreditCards();
    validate();
    if (hasActionErrors())
      return SUCCESS;
    writeUpdatesToRepository();
    return SUCCESS;
  }
  
  public void writeUpdatesToRepository()
  {

     for (Iterator i = allCreditCardsList.iterator(); i.hasNext(); )
     {
       CreditCard cc = (CreditCard) i.next();
       String id = cc.getId().toString();
       
        //if old valid is 1 & newValid is 0 then set invalidDate stamp
         if ((cc.getValid().intValue() == 1) && 
             ((new Integer((String)creditCardValidMap.get(id))).intValue() == 0)
             )
         {
           Calendar today = new GregorianCalendar();
           today.setTime(new Date());
           cc.setInvalidDate(today);
         }
       cc.setValid(new Integer((String)creditCardValidMap.get(id)));
       cc.setName((String) creditCardNameMap.get(id));
       cc.setSecurityLength(new Integer((String)securityLengthMap.get(id)));
       
       ccRepository.saveCreditCard(cc);
     }       
  }
  
  
  public void validate()
  {
  
    ShoppingCartValidator scv = new ShoppingCartValidator();

    for (Iterator i = allCreditCardsList.iterator(); i.hasNext(); )
    {
      CreditCard cc = (CreditCard) i.next();
      String id = cc.getId().toString();
      String name, valid, securityLength;

      if (!(creditCardNameMap.containsKey(id) && 
            creditCardValidMap.containsKey(id) &&
            securityLengthMap.containsKey(id)
          ))
      {
        addActionError("Invalid POST method or missing / corrupted items list.");
        return;
      }

      name = (String) creditCardNameMap.get(id);
      valid = (String) creditCardValidMap.get(id);
      securityLength = (String) securityLengthMap.get(id);

      if ((name = scv.checkText(name)) == null)
      {
        addActionError("Please specify a valid name for the new Credit Card: " +name + "\"");
        return;
      }

      if ((valid = scv.checkBoolean(valid)) == null)
      {
        addActionError("Please specify a correct valid state for the Credit Card: " +name + "\"");
        return;
      }

      if ((securityLength = scv.checkNumber(securityLength)) == null)
      {
        addActionError("Please specify a valid Security Code Length number for the Credit Card: " +name + "\"");
        return;
      }

      creditCardNameMap.put(id, name);
      creditCardValidMap.put(id, valid);
      securityLengthMap.put(id, securityLength);
    }
    //END iterator for allCreditCardsList from Directory

    // END validate
  }  
  
  
  public String newCreditCard()
  {
    validateNewCreditCard();
     if (hasActionErrors())
       return SUCCESS;
    writeNewCardToRepository();
    allCreditCardsList = getAllCreditCardsList();
    return SUCCESS;
  }
  


  private void validateNewCreditCard()
  {
    ShoppingCartValidator scv = new ShoppingCartValidator();

    if ((newCreditCardName = scv.checkText(newCreditCardName)) == null)
    {
      addActionError("Please specify a valid newCreditCardName for the new Credit Card: " +newCreditCardName + "\"");
      return;
    }

    if ((newSecurityLength = scv.checkNumber(newSecurityLength)) == null)
    {
      addActionError("Please specify a valid Security Code Length number for the Credit Card: " +newCreditCardName + "\"");
      return;
    }


  }


  public void writeNewCardToRepository()
  {
    CreditCard cc = new CreditCard();
    cc.setValid(new Integer(1));
    cc.setName(newCreditCardName);
    cc.setSecurityLength(new Integer(newSecurityLength));

    ccRepository.saveCreditCard(cc);
  }



  public void setNewCreditCardName(String newCreditCardName)
  {
    this.newCreditCardName = newCreditCardName;
  }

  public String getNewCreditCardName()
  {
    return newCreditCardName;
  }

  public void setNewSecurityLength(String newSecurityLength)
  {
    this.newSecurityLength = newSecurityLength;
  }

  public String getNewSecurityLength()
  {
    return newSecurityLength;
  }

  public void setCreditCardNameMap(Map creditCardNameMap)
  {
    this.creditCardNameMap = creditCardNameMap;
  }

  public Map getCreditCardNameMap()
  {
    if (creditCardNameMap == null)
      creditCardNameMap = new HashMap();
    return creditCardNameMap;
  }

  public void setSecurityLengthMap(Map securityLengthMap)
  {
    this.securityLengthMap = securityLengthMap;
  }

  public Map getSecurityLengthMap()
  {
    if (securityLengthMap == null)
      securityLengthMap = new HashMap();
    return securityLengthMap;
  }

  public void setCreditCardValidMap(Map creditCardValidMap)
  {
    this.creditCardValidMap = creditCardValidMap;
  }

  public Map getCreditCardValidMap()
  {
    if (creditCardValidMap == null)
      creditCardValidMap = new HashMap();
    return creditCardValidMap;
  }


  public void setAllCreditCardsList()
  {
    allCreditCardsList = creditCardDir.getCreditCards();
  }

  public List getAllCreditCardsList()
  {
    return creditCardDir.getCreditCards();
  }


}
